/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using CookComputing.XmlRpc;

namespace Whitemice.ZOGI
{
  public abstract class Company : OwnedEntity
  {
    public Company() : base()
    {
      AssociatedCategories = string.Empty;
      AssociatedCompany = string.Empty;
      AssociatedContacts = string.Empty;
      Comment = string.Empty;
      FileAs = string.Empty;
      IMAddress = string.Empty;
      URL = string.Empty;
    }
          
    public Company(IDictionary<string,object> x, int d)
      : base(x, d)
    {
    }
    
    internal override void Take(IDictionary<string,object> x, int d)
    {
      base.Take(x, d);
      AssociatedCategories = AsString(x, "associatedCategories");
			AssociatedCompany = AsString(x, "associatedCompany");
			AssociatedContacts = AsString(x, "associatedContacts");
			FileAs = AsString(x, "fileAs");
			URL = AsString(x, "url");
			IMAddress = AsString(x, "imAddress");
			Comment = AsString(x, "comment");	
    }

		public override bool CanRead { get { return true; } }
		public override bool CanDelete { get { return CanModify; } }
		public override bool CanModify { get { return HasHint("WRITE"); } }
		public bool IsPrivate { get { return HasHint("PRIVATE"); } }
		public bool IsFavorite { get { return HasHint("FAVORITE"); } }
    
		public string AssociatedCategories { get; set; }
		public string AssociatedCompany { get; set; }
		public string AssociatedContacts { get; set; }
		public string FileAs { get; set; }
		public string URL { get; set; }
		public string IMAddress { get; set; }		
		public string Comment { get; set; }
    
    public object GetCompanyValue(string name, object defaultValue)
    {
      CompanyValue cv;
      
      if ((cv = Server.Cache.LegacyXAFromCache(name, ObjectId)) != null)
        return cv.Value;
      cv = new CompanyValue(name, 1, ObjectId);
      if (defaultValue != null)
        cv.Value = defaultValue;
      Server.Cache.Store(cv);
      return cv.Value;
    }
    
    public void SetCompanyValue(string name, object newValue)
    {
      CompanyValue cv;
      
      if ((cv = Server.Cache.LegacyXAFromCache(name, ObjectId)) != null)
      {
        this.ZLOG(string.Format("Assinging value {0} to CV (objectId#{1} name:{2}", newValue, cv.ObjectId, cv.Name));
        cv.Value = newValue;
      }
      this.ZLOG(string.Format("CV (name:{0}) = {1}", name, this.GetCompanyValue(name, string.Empty))); 
    }
    
		public string Title1
		{
			get { return (GetCompanyValue("job_title", "") as string); }
			set { SetCompanyValue("job_title", value); }			
		} 

		public string Title2
		{
			get { return (GetCompanyValue("other_title1", "") as string); }
			set { SetCompanyValue("other_title1", value); }			
		}
		public string Title3
		{
			get { return (GetCompanyValue("other_title2", "") as string); }
			set { SetCompanyValue("other_title2", value); }			
		}
		
		public abstract string EMail1 { get; set; }
		
		public string EMail2
		{
		  get { return (GetCompanyValue("email2", string.Empty) as string); }
		  set { SetCompanyValue("email2", value); }
		}
		
	  public string EMail3 
		{
		  get { return (GetCompanyValue("email3", string.Empty) as string); }
		  set { SetCompanyValue("email3", value); }
		}
		
		public string EMail
		{
 			get { return EMail1; }
 			set { EMail1 = value; }
		}
		
		public string Title
		{
			get { return Title1; }
			set { Title1 = value; }
		} 
		
		public IEnumerable<Whitemice.ZOGI.Address> Addresses
		{
		  get 
		  { return Server.Cache.GetChildrenOf<Address>(ObjectId); }
		}
		
		public Address GetAddress(string type)
		{
      this.ZLOG(string.Format("Inquiry for address {0} of ObjectId#{1}", type, this.ObjectId));
		  return Server.Cache.GetAddress(ObjectId, type);
		}
		
		public IEnumerable<Whitemice.ZOGI.PhoneNumber> PhoneNumbers
		{
		  get 
		  { return Server.Cache.GetChildrenOf<PhoneNumber>(ObjectId); }
		}		
		
		public PhoneNumber GetPhoneNumber(string type)
		{
		  return Server.Cache.GetPhoneNumber(ObjectId, type);
		}		
		
		public IEnumerable<Whitemice.ZOGI.CompanyValue> CompanyValues
		{
		  get 
		  { return Server.Cache.GetChildrenOf<CompanyValue>(ObjectId); }
		}		
		
		internal XmlRpcStruct[] EncodeAddresses()
		{
			XmlRpcStruct[]          array;
			IEnumerable<Whitemice.ZOGI.Address>  addresses;
			
			addresses = this.Addresses;
			array = new XmlRpcStruct[addresses.Count()];
			for (int i = 0; i < addresses.Count(); i++)
			  array[i] = addresses.ElementAt(i).Encode();			
			return array;
		}
		
		internal XmlRpcStruct[] EncodePhoneNumbers()
		{
			XmlRpcStruct[]          array;
			IEnumerable<Whitemice.ZOGI.PhoneNumber>  phones;
			
			// TODO: Implement
			phones = this.PhoneNumbers;
			array = new XmlRpcStruct[phones.Count()];
			for (int i = 0; i < phones.Count(); i++)
			  array[i] = phones.ElementAt(i).Encode();			
			return array;
		}
		
		internal XmlRpcStruct[] EncodeCompanyValues()
		{
			XmlRpcStruct[]          array;
			IEnumerable<Whitemice.ZOGI.CompanyValue>  values;
			
			// TODO: Implement
			values = this.CompanyValues;
			array = new XmlRpcStruct[values.Count()];
			for (int i = 0; i < values.Count(); i++)
			  array[i] = values.ElementAt(i).Encode();
			return array;
		}
		
		public override XmlRpcStruct Encode()
		{
		  XmlRpcStruct  rpcStruct;
		  
		  rpcStruct = base.Encode();
 			rpcStruct.Add("associatedCategories", AssociatedCategories);
 			rpcStruct.Add("associatedCompany", AssociatedCompany);
 			rpcStruct.Add("associatedContacts", AssociatedContacts);		  
 			rpcStruct.Add("comment", Comment); 
 			rpcStruct.Add("fileAs", FileAs);
 			rpcStruct.Add("imAddress", IMAddress);
 			rpcStruct.Add("version", Version);
 			rpcStruct.Add("url", URL);
		  rpcStruct.Add("_COMPANYVALUES", EncodeCompanyValues());
 			rpcStruct.Add("_ADDRESSES", EncodeAddresses());
 			rpcStruct.Add("_PHONES", EncodePhoneNumbers());
 			return rpcStruct;
		}								
  }
}
